<template>
  <div >
    <Player/>
    <div class="banner" id="banner1">
      <a  @click="$router.push('/dashboard')"   style="color: white;font-size: small;position: relative;top:70px;left: 1300px;font-weight: bold"  class="banner-title">首页</a>
      <a  @click="$router.push('/myblog')"   style="color: white;font-size: small;position: relative;top:70px;left: 1300px;font-weight: bold"  class="banner-title">我的博客</a>
      <a  @click="$router.push('/show')"   style="color: white;font-size: small;position: relative;top:70px;left: 1300px;font-weight: bold"  class="banner-title">江湖</a>
      <a  @click="$router.push('/blogStore')"   style="color: white;font-size: small;position: relative;top:70px;left: 1300px;font-weight: bold"  class="banner-title">商城</a>

      <h1 class="banner-title" style="margin-top: 10px">留言</h1>
    </div>
  <div style=" margin-bottom: 80px"  >

    <el-card style="margin-top: 350px" class="blog-container">
      <div style="padding: 20px; color: #888">
        <div>
          <el-input type="textarea" :rows="3" v-model="entity.content"></el-input>
          <div style="text-align: right; padding: 10px"><el-button type="primary" @click="save" >留言</el-button></div>
        </div>
      </div>

      <div style="display: flex; padding: 20px" v-for="item in messages">
        <div style="text-align: center; flex: 1">
          <el-image :src="item.avatar" style="width: 60px; height: 60px; border-radius: 50%"></el-image>
        </div>
        <div style="padding: 0 10px; flex: 5">
          <div><b style="font-size: 14px">{{ item.username }}</b></div>
          <div style="padding: 10px 0; color: #888">
            {{ item.content }}
            <el-button type="text" size="mini" @click="del(item.id)" v-if="item.username === user.username">删除</el-button>
          </div>
          <div style="background-color: #eee; padding: 10px" v-if="item.parentMessage">{{ item.username }}：{{ item.parentMessage.content }}</div>
          <div style="color: #888; font-size: 12px">
            <span>{{ item.time  }}</span>
            <el-button type="text" style="margin-left: 20px" @click="reReply(item.id)">回复</el-button>
          </div>
        </div>
      </div>

      <el-dialog title="回复信息":visible.sync="dialogFormVisible" width="30%">
        <el-form :model="entity" label-width="80px">
          <el-form-item label="内容">
            <el-input v-model="entity.reply" autocomplete="off" type="textarea" :rows="3"></el-input>
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="reply">确 定</el-button>
        </template>
      </el-dialog>
    </el-card>
  </div>

    <div app padless absolute style="margin-top: -90px">
      <div class="footer-wrap">
        <div>©2021 - {{ new Date().getFullYear() }} By chenchao</div>
        <a href="" target="_blank">
          邮箱：2776895893@qq.com
        </a>
      </div>
    </div>
  </div>
</template>

<script>
  import '../../public/ribbon.js'
  import request from "@/utils/request";
  import effect from '../views/Effect'
  import "../assets/index.css";
  import Player from '../components/zw-player/player.vue'
  export default {
    name: "Message",
    components:{effect,Player},
    data() {
      return {
        user: {},
        messages: [],
        dialogFormVisible: false,
        isCollapse: false,
        entity: {}
      }
    },
    created() {
      this.user = sessionStorage.getItem("user") ? JSON.parse(sessionStorage.getItem("user")) : {};
      this.entity.username=this.user.username
      this.entity.foreignId=0
      this.loadMessage();


    },
    methods: {
      loadMessage() {
        // 如果是留言的话，就写死=0
        // 如果是 评论，则需要设置 当前被评论的模块的id作为foreignId
        let foreignId = 0;
        this.entity.foreignId=0
        this.entity.username=this.user.username
        request.get("/message/foreign/" + foreignId).then(res => {
          this.messages = res.data;
        })
      },
      cancel() {
        this.dialogFormVisible = false;
        this.entity = {};
      },
      reReply(id) {
        this.dialogFormVisible = true;
        this.entity.foreignId=0
        this.entity.parentId = id;
      },
      reply() {
        this.entity.content = this.entity.reply;
        this.entity.foreignId=0
        this.save();
      },

      save() {
        if (!this.user.username) {
          this.$message({
            message: "请登录",
            type: "warning"
          });
          return;
        }
        if (!this.entity.content) {
          this.$message({
            message: "请填写内容",
            type: "warning"
          });
          return;
        }
        // 如果是评论的话，在 save的时候要注意设置 当前模块的id为 foreignId。也就是  entity.foreignId = 模块id
        request.post("/message", this.entity).then(res => {
          this.entity.foreignId=0
          if (res.code === 20000) {
            this.$message({
              message: "评论成功",
              type: "success"
            });
          } else {
            this.$message({
              message: res.msg,
              type: "error"
            });
          }
          this.entity = {}
          this.loadMessage();
          this.dialogFormVisible = false;
        })
      },
      del(id) {
        request.delete("/message/" + id).then(res => {
          this.$message({
            message: "删除成功",
            type: "success"
          });
          this.loadMessage()
        })
      }
    }
  }
</script>


<style>
  #banner1{
    background: url('https://www.static.talkxj.com/config/a6f141372509365891081d755da963a1.png') no-repeat center center/cover;
  }
  .footer-wrap {
    width: 100%;
    line-height: 2;
    position: relative;
    padding: 40px 20px;
    color: #eee;
    font-size: 14px;
    text-align: center;
    background: linear-gradient(-45deg, #ee7752, #ce3e75, #23a6d5, #23d5ab);
    background-size: 400% 400%;
    animation: Gradient 10s ease infinite;
  }
  .footer-wrap a {
    color: #eee !important;
  }

  @keyframes Gradient {
    0% {
      background-position: 0 50%;
    }
    50% {
      background-position: 100% 50%;
    }
    100% {
      background-position: 0 50%;
    }
  }
</style>
